import Router from 'koa-router';
const router = new Router();
import query from '../utils/query'
import jwt from 'jsonwebtoken'
router.prefix('/v1');

const login = async (ctx:Router.RouterContext, next:any) => {
    let {user,pass}:any=ctx.request.body;
    if(!user&&!pass){
        ctx.body={
            code:0,
            msg:'缺少参数'
        }
    }
    let sql=await query(`select * from user where name='${user}' and pwd='${pass}'`)
    if(sql){
        // 注册token
        let token =jwt.sign({user,pass},'logInf',{'expiresIn':'50s'})
        ctx.body={
            code:200,
            msg:'登录成功',
            token
        }
    }else{
        ctx.body={
            code:100,
            msg:'登录失败',
        }
    }
    
};
router.post('/login', login);
const string = async (ctx:Router.RouterContext, next:any) => {
    ctx.body = 'welcome to Koa!';
};
router.get('/string', string);
router.post('/string', string);

export default router;
